package Service;

import Dao.Impl.CarDAOImpl;
import Dao.Impl.CustomerDAOImpl;
import Dao.Impl.RentalRecordDAOImpl;
import pojo.Car;
import pojo.Customer;
import pojo.RentalRecord;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class UserService {
    private CustomerDAOImpl customerDAOImpl;
    private CarDAOImpl carDAOImpl;
    private RentalRecordDAOImpl rentalRecordDAOImpl;

    public UserService() throws Exception {
        customerDAOImpl = new CustomerDAOImpl();
        carDAOImpl = new CarDAOImpl();
        rentalRecordDAOImpl = new RentalRecordDAOImpl();
    }

    //用户在用户界面更新个人信息
    public Boolean updateUser(Customer customer) {
        Boolean flag = null;
        try {

            flag = customerDAOImpl.updateCustomer(customer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    //根据用户ID获取用户信息
    public Customer getCustomerById(int userId) throws Exception {
        return customerDAOImpl.getCustomerById(userId);
    }

    //获取所有车辆信息展示在用户窗口
    public List<Car> getAllCars() throws Exception {
        return carDAOImpl.getAllCars();
    }

    //通过用户id获取用户订单记录
    public List<RentalRecord> getRentalRecordsByCustomerId(int customerId) throws Exception {
        List<RentalRecord> orderList = rentalRecordDAOImpl.getRentalRecordsByCustomerId(customerId);
        return orderList;
    }

    //用户在用户界面还车更新订单信息
    public Boolean updateRentalRecord(RentalRecord rentalRecord) throws Exception {
        // 获取当前时间
        Date d = new Date();

        long time = d.getTime();

        // 2、格式化日期对象，和时间 毫秒值。
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endRentalTime = dateFormat.format(time);

        rentalRecord.setRental_status("已完成");
        rentalRecord.setEnd_date(endRentalTime);
        return rentalRecordDAOImpl.updateRentalRecord(rentalRecord);
    }

    //用户在用户界面租车时更新订单的信息
    public Boolean updateRentalRecord(int carId, int userId) throws Exception {
        // 获取当前时间
        Date startDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startRentalTime = dateFormat.format(startDate);
        // 获取加一天后的时间
        Date endDate = new Date();
        LocalDate startLocalDate = LocalDate.parse(startRentalTime.substring(0, 10));
        LocalDate endRentalTime = startLocalDate.plusDays(1); // 加一天
        // 计算天数差异
        long daysDiff = ChronoUnit.DAYS.between(startLocalDate, endRentalTime);

        //获取车辆信息，进行费用计算
        Car car = new Car();
        //进行数据库查询，得到car的信息
        car = carDAOImpl.getCarById(carId);
        System.out.println("Userservicce:"+car);
        int fee = ((int)daysDiff) * car.getRental_rate();

        String rental_status = "进行中";

        //更新订单信息并存入数据库
        RentalRecord rentalRecord = new RentalRecord();
        rentalRecord.setCar_id(carId);
        rentalRecord.setCustomer_id(userId);
        rentalRecord.setStart_date(startRentalTime);
        rentalRecord.setRental_status(rental_status);
        rentalRecord.setEnd_date(String.valueOf(endRentalTime));
        rentalRecord.setRental_fee(fee);
        rentalRecord.setRental_days((int)daysDiff);

        //更新数据库，增加订单
        return rentalRecordDAOImpl.addRentalRecord(rentalRecord);
    }

    //用户在用户界面还车时进行更新信息
    public boolean updateCarByOrder(RentalRecord rentalRecord) throws Exception {
        String startCarStatus = "已租";
        String changeCarStatus = "可租";
        int carId = rentalRecord.getCar_id();
        return carDAOImpl.updateCarByOrder(carId,startCarStatus,changeCarStatus);
    }

    //租车时更新个人信息，租车数量
    public Boolean updateUserByRent(Customer customer) throws Exception {
        customer.setRent_car_number(customer.getRent_car_number()+1);
        return customerDAOImpl.updateUserByRent(customer);
    }

    //还车时更新个人信息，租车数量，金钱余额
    public Boolean updateUserByReturn(Customer customer,RentalRecord order) throws Exception {
        customer.setRent_car_number(customer.getRent_car_number()-1);
        //进行金额相减
        RentalRecord rentalRecord = rentalRecordDAOImpl.SearchRental(order);
        int money = rentalRecord.getRental_fee();
        int leftmoney = customer.getLeft_money()-money;

        customer.setLeft_money(leftmoney);
        return customerDAOImpl.updateUserByReturn(customer);
    }
}
